<?php

namespace App\Admin\Controllers;

use App\Models\Withdraw;
use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Layout\Column;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Layout\Row;
use Illuminate\Http\Request;
use App\Admin\Extensions\Has_pay;
use App\Admin\Extensions\Has_failed;

class WithdrawlistController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('提现申请')
            ->description('提现申请')
            ->row(function (Row $row) {
                $row->column(3, function (Column $column) {
                    $total = Withdraw::where('status', 2)->sum('money');
                    $infoBox = new InfoBox('提现总额', 'cny', 'aqua', '#', $total);
                    $column->append($infoBox);
                });
                $row->column(3, function (Column $column) {
                    $total = Withdraw::where([['type', 1],['status',2]])->sum('money');
                    $infoBox = new InfoBox('支付宝总额', 'cny', 'aqua', '#', $total);
                    $column->append($infoBox);
                });
                $row->column(3, function (Column $column) {
                    $total = Withdraw::where([['type', 2],['status',2]])->sum('money');
                    $infoBox = new InfoBox('微信总额', 'cny', 'aqua', '#', $total);
                    $column->append($infoBox);
                });
                $row->column(3, function (Column $column) {
                    $today_total = Withdraw::where('status', 2)->whereDate('updated_at', date('Y-m-d'))->sum('money');
                    $infoBox = new InfoBox('今日转账总额', 'cny', 'aqua', '#', $today_total);
                    $column->append($infoBox);
                });

            })
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('提现申请')
            ->description('提现申请')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('提现申请')
            ->description('提现申请')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Withdraw);

        $grid->model()->wherein('status', [2,3]);
        $grid->id('Id');
        $grid->user()->phone('申请人');
        $grid->money('提现金额');
        $grid->water_num('流水号');
        $grid->type('提现类型')->display(function($value){
            switch($value)
            {
                case '1':
                    return '支付宝';
                    break;
                case '2':
                    return '微信';
                    break;
                case '3':
                    return '银行卡';
                    break;
                default:
                    break;
            }
        });
        $grid->status('状态')->display(function($value){
            switch($value)
            {
                case '1':
                    return "<span class='label label-warning'>待打款</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>已通过</span>";
                    break;
                case '3':
                    return "<span class='label label-danger'>未通过</span>";
                    break;
                default:
                    break;
            }
        });
        $grid->remark('备注');
        $grid->created_at('申请时间');
        $grid->disableCreateButton();
        $grid->disableRowSelector();

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();            
            $row = $actions->row;

            $actions->append("<a href='/admin/userinfo/" . $actions->getKey() . "'>" . '<button type="button" class="btn btn-primary">提现详情</button>' . "</a>");
                    
        });

        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('type', '提现至')->radio([
                1 => '支付宝',
                2 => '微信',
                3 => '银行卡',
            ]);
            $filter->equal('status', '状态')->radio([
                1 => '待打款',
                2 => '已通过',
                3 => '未通过',
            ]);
            $filter->scope('支付宝')->where('type', 1);
            $filter->scope('今日新增', '今日新增')->whereDate('created_at', date('Y-m-d'));

        });
        return $grid;
    }
    //提现人信息
    public function userinfo(Request $request,$id)
    {
        $withdraw = Withdraw::where('id',$id)->with('user')->first();
        $headers = ['提现人', '提现金额', '提现类型','收款账号','申请时间','打款时间'];
        // dd($withdraw);
        $ret = [];
        switch($withdraw->type)
        {
            case '1':
                $type = '支付宝';
                $account = $withdraw->user->alipay;
                break;
            case '2':
                $type = '微信';
                $account = $withdraw->user->wechat;
                break;
            case '3':
                $type = '银行卡';
                $account = $withdraw->user->bank_num;
                break;
            default:
                break;    
        }
        
        $ret[] = [$withdraw->user->phone, $withdraw->money, $type,$account,$withdraw->created_at,$withdraw->updated_at];
        
        $table = new Table($headers, $ret);

        $box = new Box('提现详情', $table->render());
        $box->style('success');
        $box->solid();
        $box->collapsable();
        $box->back();
        $content = new Content();        
        return $content
            ->header('提现详情')
            ->description('详情')
            ->body($box->render());
    }
    //通过打款
    public function has_pay(Request $request)
    {       
        $id = $request->id;
        $Withdraw = Withdraw::findOrFail($id);
        $Withdraw->status = 2;
        $res = $Withdraw->save();
        if($res){
            return $this->ajax_success('操作成功');
        }else{
            return $this->ajax_error('操作失败');
        }
        
    }
    //打款不通过
    public function has_failed(Request $request)
    {       
        $id = $request->id;
        $Withdraw = Withdraw::findOrFail($id);
        $Withdraw->status = 3;
        $Withdraw->remark = $request->remark;
        $res = $Withdraw->save();
        if($res){
            return $this->ajax_success('操作成功');
        }else{
            return $this->ajax_error('操作失败');
        }
        
    }
}
